
function map(api)
{
	this.camera = new camera()
	this.entities = {};
	this.actions = [];
	this.canvas = null;
	this.ctx = null;
	this.turn = 0;
	this.mousex = 0;
	this.mousey = 0;
	this.selected = null;
	this.camera = new camera();
	this.api = api;
	
	this.update = function update()
	{
		this.canvas = document.getElementById("gamemap");
		this.ctx = this.canvas.getContext("2d");
		this.draw_map();
	}
	
	this.draw_controls = function draw_controls()
	{
		this.ctx.save();
		this.ctx.translate(0,0);
		this.ctx.scale(1.0,1,0);  

		this.draw_arrow();

		this.ctx.restore(); 
	}
	
	this.draw_arrow  = function draw_arrow()
	{
		var i = 1;
	}

	this.draw_selected = function draw_selected()
	{
		if(selected != null)
		{
			ctx.beginPath();  
			ctx.strokeStyle = "black";
			ctx.arc(selected.x + 2,selected.y + 2,5,0,Math.PI*2,true); // Outer circle 
			ctx.stroke();
		}
	}

	this.detect_over = function detect_over(x,y)
	{
		for(var i in space_items)
		{
			var delta_x = x - space_items[i].x;
			var delta_y = y - space_items[i].y;
			if (5 > delta_x && delta_x > 0)
			{
				if (5 > delta_y && delta_y > 0)
				{
					$('#over').html(object_2_string(space_items[i]));
					selected = space_items[i];
					draw_map();
				}
			}
		}
	}

	this.draw_action = function draw_action(action)
	{
		if (action.type == 'attack')
		{
			var attacker = space_items[action['atk']];
			var target = space_items[action['tgt']];
			ctx.strokeStyle = "rgba(230, 0, 200, 0.3)";
			ctx.beginPath();
			ctx.moveTo(attacker.x, attacker.y);
			ctx.lineTo(target.x, target.y);
			ctx.stroke();
		}

		else if (action.type == 'move')
		{
			var ship = space_items[action['idx']];
			var target_x = action['x'];
			var target_y = action['y'];
			ctx.strokeStyle = "rgba(200, 200, 200, 0.3)";
			ctx.beginPath();
			ctx.moveTo(ship.x, ship.y);
			ctx.lineTo(target_x, target_y);
			ctx.stroke();
		}
	}



	this.draw_map = function draw_map()
	{
		this.ctx.clearRect(0,0,960,400);
		this.ctx.save();
		this.ctx.translate(this.camera.x,this.camera.y);
		this.ctx.scale(this.camera.zoom,this.camera.zoom);  

		//draw_selected();

		//$.each(actions, function(i,item){
			//draw_action(item);
		//});


		for(var i in this.api.entities)
		{
			this.draw_entities(this.api.entities[i]);
		}

		this.ctx.restore(); 
	}

	this.draw_entities = function draw_entities(ent)
	{
		if(ent.type == 'ship')
		{
			//ships.push(item);
			this.draw_ship(ent);
		}
		else if(ent.type == 'base')
		{
			this.draw_base(ent);
		}
		else if(ent.type == 'asteroid')
		{
			this.draw_asteroid(ent);
		}
	}

	this.draw_ship = function draw_ship(ship)
	{
		if(this.api.player == ship.player) 
		{
			this.ctx.fillStyle = "green";
		}
		else 
		{
			this.ctx.fillStyle = "red";
		}
		
		this.ctx.fillRect (ship.x, ship.y, 2, 2);
	}

	this.draw_base = function draw_base(base)
	{
		if(this.api.player == base.player) 
		{
			this.ctx.fillStyle = "green";
		}
		else 
		{
			this.ctx.fillStyle = "red";
		}
		
		this.ctx.fillRect (base.x, base.y, 7, 7);
	}

	this.draw_asteroid = function draw_asteroid(item)
	{
		this.ctx.fillStyle = "rgba(120, 120, 120, 0.5)";
		this.ctx.fillRect (item.x, item.y, 4, 4);
	}
}

function camera()
{
	this.x = 0;
	this.y = 0;
	this.zoom = 1.0;
}

